package com.xinqi.modules.chat.chat.friend.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.xinqi.common.base.enums.YesNoNumberEnum;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.base.response.R;
import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.common.satoken.utils.LoginHelper;
import com.xinqi.modules.chat.chat.friend.dto.req.ChatFriendApplyAutoDTO;
import com.xinqi.modules.chat.chat.friend.dto.req.ChatFriendApplyCreateDTO;
import com.xinqi.modules.chat.chat.friend.dto.req.ChatFriendApplyFindListQueryDTO;
import com.xinqi.modules.chat.chat.friend.dto.req.ChatFriendApplyQueryDTO;
import com.xinqi.modules.chat.chat.friend.dto.rsp.ChatFriendApplyResultDTO;
import com.xinqi.modules.chat.chat.friend.dto.rsp.FreindApplyResultDTO;
import com.xinqi.modules.chat.chat.friend.service.ChatFriendApplyService;
import com.xinqi.modules.chat.enums.apply.FriendApplyComnonEnum;
import com.xinqi.modules.chat.enums.apply.FriendApplyStatusEnum;
import com.xinqi.modules.user.user.dto.req.PhoneLinkerParamDTO;
import com.xinqi.modules.user.user.dto.req.SearchFriendDTO;
import com.xinqi.modules.user.user.dto.rsp.SearchFriendResultDTO;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;

/**
 * WEB -- 好友申请
 *
 * @version V1.0
 * @Title:
 * @ClassName: com.xinqi.modules.chat.chat.friend.controller.web.ChatFriendApplyWebController.java
 * @Description:
 * @Copyright 星齐科技（云南）有限公司 - Powered By 技术部
 * @author: yyzhen
 * @date: 2023/5/19 10:05
 */
@Validated
@RestController
@RequestMapping("/friend-apply")
@RequiredArgsConstructor
public class ChatFriendApplyTempController {
    private final ChatFriendApplyService chatFriendApplyService;

    @Operation(summary = "搜索好友")
    @PostMapping("search")
    public R<SearchFriendResultDTO> search(@Validated @RequestBody SearchFriendDTO dto) {
        List<SearchFriendResultDTO> userList = chatFriendApplyService.selectUsersByKeyword(dto);
        if (CollectionUtil.isEmpty(userList) || userList.size() > 1) {
            return R.error(MessageUtils.message("sys.user.info.not.blank"));
        } else {
            SearchFriendResultDTO resultDTO = userList.get(0);
            //补全搜索来源/是否好友
            if (resultDTO.getXinqiNumber().equals(dto.getKeywords())) {
                //手机号脱敏了，采用新奇号验证
                resultDTO.setComnon(FriendApplyComnonEnum.XIN_QI_NUMBER.getCode());
            } else {
                resultDTO.setComnon(FriendApplyComnonEnum.PHONE_NUMBER.getCode());
            }
            if (chatFriendApplyService.friedCheck(resultDTO.getId(), LoginHelper.getUserId())) {
                resultDTO.setFriendflag(YesNoNumberEnum.YES.getCode());
            } else {
                resultDTO.setFriendflag(YesNoNumberEnum.NO.getCode());
            }
            return R.ok(resultDTO);
        }
    }

    @Operation(summary = "申请添加好友")
    @PostMapping("/add")
    public R<Long> add(@Validated @RequestBody ChatFriendApplyCreateDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        return R.data(chatFriendApplyService.insert(dto), MessageUtils.message("chat.friend.apply.add.success"));
    }

    @Operation(summary = "好友申请-分页")
    @GetMapping("/page")
    public R<PageInfo<ChatFriendApplyResultDTO>> page(Param pageable, ChatFriendApplyQueryDTO query) {
        query.setOperationUserId(LoginHelper.getUserId());
        PageInfo<ChatFriendApplyResultDTO> page = chatFriendApplyService.findPage(pageable, query);
        return R.data(page);
    }

    @Operation(summary = "好友申请-列表（被申请/自己申请）")
    @PostMapping("/list")
    public R<FreindApplyResultDTO> list(@RequestBody ChatFriendApplyFindListQueryDTO query) {
        query.setOperationUserId(LoginHelper.getUserId());
        return R.data(chatFriendApplyService.selectList(query));
    }

    @Operation(summary = "好友申请未读消息")
    @GetMapping("/unread")
    public R<Integer> unread() {
        Integer result = chatFriendApplyService.applyFriendOnRead(LoginHelper.getUserId());
        return R.data(result);
    }

    @Operation(summary = "清除好友未读消息")
    @PutMapping("clearUnread")
    public R<Boolean> clearUnread() {
        Boolean result = chatFriendApplyService.applyFriendOnReadClean(LoginHelper.getUserId());
        return R.data(result);
    }

    @Operation(summary = "好友申请同意/拒绝")
    @PutMapping("auto-friend")
    public R<Boolean> autoFriend(@Validated @RequestBody ChatFriendApplyAutoDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        Boolean result = chatFriendApplyService.autoFriend(dto);
        if (Objects.equals(FriendApplyStatusEnum.ONE, dto.getStatus())) {
            return R.data(result, MessageUtils.message("chat.friend.apply.auto.success"));
        }
        return R.data(result, MessageUtils.message("chat.friend.apply.auto.fail"));
    }

    @Operation(summary = "好友申请-详情")
    @GetMapping("/info/{id}")
    public R<ChatFriendApplyResultDTO> info(@NotNull(message = "{chat.friend.apply.id.not.blank}") @PathVariable("id") Long id) {
        ChatFriendApplyResultDTO dto = chatFriendApplyService.findById(id, LoginHelper.getUserId());
        return R.data(dto);
    }

    @Operation(summary = "可能认识的人")
    @PostMapping("potential")
    public R<List<SearchFriendResultDTO>> potential() {
        return R.ok(chatFriendApplyService.myPotential(LoginHelper.getUserId()));
    }

    @Operation(summary = "手机联系人")
    @PostMapping("phoneLinker")
    public R<List<SearchFriendResultDTO>> phoneLinker(@Validated @RequestBody PhoneLinkerParamDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        return R.ok(chatFriendApplyService.phoneLinkerList(dto));
    }

    @Operation(summary = "删除好友申请")
    @DeleteMapping("/{id}")
    public R<String> delete(@NotNull(message = "{chat.friend.apply.id.not.blank}") @PathVariable("id") Long id) {
        if (chatFriendApplyService.deleteByApplyId(id)) {
            return R.ok(MessageUtils.message("common.remove.success"));
        } else {
            return R.ok(MessageUtils.message("common.remove.fail"));
        }
    }

}
